package com.luoxiang.socialandroid.dialog

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.databinding.DialogSuspendedWindowBinding
import com.lxj.xpopup.impl.FullScreenPopupView

class SuspendedWindowDialog(context: Context) : FullScreenPopupView(context) {
    private val mViewBinding: DialogSuspendedWindowBinding by lazy {
        DialogSuspendedWindowBinding.bind(
            popupImplView
        )
    }

    override fun getImplLayoutId() = R.layout.dialog_suspended_window

    override fun onCreate() {
        super.onCreate()
        mViewBinding.imageCancel.setOnClickListener {
            dismiss()
        }
        mViewBinding.imageOpen.setOnClickListener {
            requestSettingCanDrawOverlays()
            dismiss()
        }
    }

    //权限打开
    private fun requestSettingCanDrawOverlays() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8.0以上
            (context as Activity).startActivity(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION))
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //6.0-8.0
            val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
            intent.data = Uri.parse("package:" + context.packageName)
            (context as Activity).startActivity(intent)
        } else { //4.4-6.0以下
            //无需处理了
        }
    }

}